package admin

import (
	"bytes"
	"encoding/json"
	"fmt"
	"mime/multipart"
	"mogu-go-v2/common"
	"mogu-go-v2/controllers/base"
	"mogu-go-v2/models"
	"mogu-go-v2/models/maps"
	"mogu-go-v2/models/page"
	"mogu-go-v2/models/vo"
	"mogu-go-v2/service"
	"reflect"
	"strconv"
	"strings"
	"time"
	"net/http"
	urlUtil "net/url"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/rs/xid"
	"io/ioutil"
)

/**
 *
 * @author  镜湖老杨
 * @date  2021/1/11 9:57 上午
 * @version 1.0
 */
type BlogSpiderRestApi struct {
	base.BaseController
}


func (c *BlogSpiderRestApi) GetList() {
	var blogSpiderVO vo.BlogSpiderVO
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &blogSpiderVO)
	if err != nil {
		panic(err)
	}
	where := "status=?"
	if strings.TrimSpace(blogSpiderVO.Keyword) != "" {
		where += " and title like '%" + strings.TrimSpace(blogSpiderVO.Keyword) + "%'"
	}
	if blogSpiderVO.TagUid != "" {
		where += " and tag_uid like '%" + blogSpiderVO.TagUid + "%'"
	}
	if blogSpiderVO.BlogSortUid != "" {
		where += " and blog_sort_uid like '%" + blogSpiderVO.BlogSortUid + "%'"
	}
	levelKeyword := common.InterfaceToString(blogSpiderVO.LevelKeyword)
	if blogSpiderVO.LevelKeyword != "" {
		where += " and level='" + levelKeyword + "'"
	}
	if blogSpiderVO.IsPublish != "" {
		where += " and is_publish='" + blogSpiderVO.IsPublish + "'"
	}
	if blogSpiderVO.IsOriginal != "" {
		where += " and is_original='" + blogSpiderVO.IsOriginal + "'"
	}
	order := sortDesc
	if blogSpiderVO.OrderByAscColumn != "" {
		order = common.Camel2Case(blogSpiderVO.OrderByAscColumn) + " asc"
	} else if blogSpiderVO.OrderByDescColumn != "" {
		order = common.Camel2Case(blogSpiderVO.OrderByDescColumn) + " desc"
	} else if blogSpiderVO.UserSort == 0 {
		order = "create_time desc"
	}
	var pageList []models.BlogSpider
	var total int64
	c.Wg.Add(2)
	go func() {
		common.DB.Model(&models.BlogSpider{}).Where(where, 1).Count(&total)
		c.Wg.Done()
	}()
	go func() {
		common.DB.Where(where, 1).Offset((blogSpiderVO.CurrentPage - 1) * blogSpiderVO.PageSize).Limit(blogSpiderVO.PageSize).Order(order).Find(&pageList)
		c.Wg.Done()
	}()
	c.Wg.Wait()
	if len(pageList) == 0 {
		page := map[string]interface{}{}
		page["records"] = pageList
		c.SuccessWithData(page)
		return
	}
	var s []string
	var sortUids []string
	var tagUids []string
	for _, item := range pageList {
		if item.FileUid != "" {
			s = append(s, item.FileUid)
		}
		if item.BlogSortUid != "" {
			sortUids = append(sortUids, item.BlogSortUid)
		}
		if item.TagUid != "" {
			tagUidsTemp := strings.Split(item.TagUid, ",")
			for _, itemTagUid := range tagUidsTemp {
				tagUids = append(tagUids, itemTagUid)
			}
		}
	}
	pictureList := map[string]interface{}{}
	fileUids := strings.Join(s, ",")
	if fileUids != "" {
		pictureList = service.FileService.GetPicture(fileUids, ",")
	}
	picList := common.WebUtil.GetPictureMap(pictureList)
	var sortList []models.BlogSort
	var tagList []models.Tag
	c.Wg.Add(1)
	go func() {
		if len(sortUids) > 0 {
			common.DB.Find(&sortList, sortUids)
		}
		c.Wg.Done()
	}()
	c.Wg.Add(1)
	go func() {
		if len(tagUids) > 0 {
			common.DB.Find(&tagList, tagUids)
		}
		c.Wg.Done()
	}()
	c.Wg.Wait()
	sortMap := map[string]models.BlogSort{}
	tagMap := map[string]models.Tag{}
	pictureMap := map[string]string{}
	for _, item := range sortList {
		sortMap[item.Uid] = item
	}
	for _, item := range tagList {
		tagMap[item.Uid] = item
	}
	for _, item := range picList {
		pictureMap[item["uid"].(string)] = item["url"].(string)
	}
	for i, item := range pageList {
		if item.BlogSortUid != "" {
			pageList[i].BlogSort = sortMap[item.BlogSortUid]
		}
		if item.TagUid != "" {
			tagUidsTemp := strings.Split(item.TagUid, ",")
			var tagListTemp []models.Tag
			for _, tag := range tagUidsTemp {
				tagListTemp = append(tagListTemp, tagMap[tag])
			}
			pageList[i].TagList = tagListTemp
		}
		if item.FileUid != "" {
			pictureUidsTemp := strings.Split(item.FileUid, ",")
			var pictureListTemp []string
			for _, picture := range pictureUidsTemp {
				pictureListTemp = append(pictureListTemp, pictureMap[picture])
			}
			pageList[i].PhotoList = pictureListTemp
		}
	}
	iPage := page.IPage{
		Records: pageList,
		Total:   total,
		Size:    blogSpiderVO.PageSize,
		Current: blogSpiderVO.CurrentPage,
	}
	c.SuccessWithData(iPage)
}

func (c *BlogSpiderRestApi) Edit() {
	var blogSpiderVO vo.BlogSpiderVO
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &blogSpiderVO)
	if err != nil {
		panic(err)
	}
	var blogSpider models.BlogSpider
	common.DB.Where("uid=?", blogSpiderVO.Uid).Find(&blogSpider)
	var count int64
	common.DB.Model(&models.BlogSpider{}).Where("status=? and level= ?", 1, blogSpiderVO.Level).Count(&count)
	if !reflect.DeepEqual(blogSpider, models.BlogSpider{}) {
		if blogSpider.Level != blogSpiderVO.Level {
			count++
		}
	}

	var admin models.Admin
	common.DB.Where("uid = ?", c.GetAdminUid()).Find(&admin)
	blogSpider.AdminUid = admin.Uid

	blogSpider.Title = blogSpiderVO.Title
	blogSpider.Summary = blogSpiderVO.Summary
	blogSpider.Content = blogSpiderVO.Content
	blogSpider.TagUid = blogSpiderVO.TagUid
	blogSpider.BlogSortUid = blogSpiderVO.BlogSortUid
	blogSpider.FileUid = blogSpiderVO.FileUid
	blogSpider.Level = blogSpiderVO.Level
	blogSpider.IsOriginal = blogSpiderVO.IsOriginal
	blogSpider.IsPublish = blogSpiderVO.IsPublish
	err1 := common.DB.Save(&blogSpider).Error
	if err1 != nil {
		fmt.Println(err1)
	}
	c.SuccessWithMessage("更新成功")
}

func (c *BlogSpiderRestApi) Delete() {
	var blogVO vo.BlogVO
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &blogVO)
	if err != nil {
		panic(err)
	}
	var blog models.Blog
	common.DB.Where("uid=?", blogVO.Uid).Find(&blog)
	save := common.DB.Model(&blog).Select("status").Update("status", 0).Error
	if save == nil {
		blogUidList := []string{blogVO.Uid}
		service.SubjectItemService.DeleteBatchSubjectItemByBlogUid(blogUidList)
	}
	common.RedisUtil.Delete("BLOG_LEVEL:" + strconv.Itoa(blog.Level))
	c.SuccessWithMessage("删除成功")
}

func (c *BlogSpiderRestApi) Add() {
	var blogVO vo.BlogVO
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &blogVO)
	if err != nil {
		panic(err)
	}
	var count int64
	common.DB.Model(&models.Blog{}).Where("status=? and level= ?", 1, blogVO.Level).Count(&count)
	addVerdictResult := service.BlogService.AddVerdict(count, blogVO.Level)
	if addVerdictResult != "" {
		c.ErrorWithMessage(addVerdictResult)
		return
	}
	var blog models.Blog
	projectName, _ := service.SysParamService.GetSysParamsValueByKey("PROJECT_NAME")
	if blogVO.IsOriginal == "1" {
		var admin models.Admin
		common.DB.Where("uid = ?", c.GetAdminUid()).Find(&admin)
		if !reflect.DeepEqual(admin, models.Admin{}) {
			if admin.NickName != "" {
				blog.Author = admin.NickName
			} else {
				blog.Author = admin.UserName
			}
			blog.AdminUid = admin.Uid
		}
		blog.ArticlesPart = projectName
	} else {
		blog.Author = blogVO.Author
		blog.ArticlesPart = blogVO.ArticlesPart
	}
	blog.Uid = xid.New().String()
	blog.Title = blogVO.Title
	blog.Summary = blogVO.Summary
	blog.Content = blogVO.Content
	blog.TagUid = blogVO.TagUid
	blog.BlogSortUid = blogVO.BlogSortUid
	blog.FileUid = blogVO.FileUid
	blog.Level = blogVO.Level
	blog.IsOriginal = blogVO.IsOriginal
	blog.IsPublish = blogVO.IsPublish
	blog.OpenComment = blogVO.OpenComment
	blog.Type = blogVO.Type
	blog.OutsideLink = blogVO.OutsideLink
	blog.Oid = blogVO.Oid
	common.DB.Create(&blog)
	common.RedisUtil.Delete("BLOG_LEVEL:" + strconv.Itoa(blog.Level))
	c.SuccessWithMessage("新增成功")
}

func (c *BlogSpiderRestApi) UploadLocalBlog() {
	fileDatas, _ := c.GetFiles("filedatas")
	m := service.SystemConfigService.GetConfig()
	systemConfig := m["data"].(models.SystemConfig)
	if systemConfig == (models.SystemConfig{}) {
		c.ErrorWithMessage("系统配置不存在,请重新登录")
		return
	}
	if systemConfig.UploadQiNiu == "1" && (systemConfig.QiNiuPictureBaseUrl == "" || systemConfig.QiNiuAccessKey == "") {
		c.ErrorWithMessage("请先设置七牛云")
		return
	}
	var fileList []*multipart.FileHeader
	var fileNameList []string
	for _, file := range fileDatas {
		fileOriginalName := file.Filename
		if !strings.HasSuffix(fileOriginalName, ".md") {
			c.ErrorWithMessage("目前仅支持Markdown文件")
			fileList = []*multipart.FileHeader{}
			break
		}
		fileList = append(fileList, file)
		fileNameList = append(fileNameList, common.FileUtil.GetFileName(fileOriginalName))
	}
	if len(fileList) == 0 {
		c.ErrorWithMessage("请选中需要上传的Markdown文件")
		return
	}
	var fileContentList []string
	for _, mutipartFile := range fileList {
		file, _ := mutipartFile.Open()
		fil := make([][]byte, 0)
		var b int64 = 0
		for {
			buffer := make([]byte, 1024)
			n, err := file.ReadAt(buffer, b)
			b += int64(n)
			fil = append(fil, buffer)
			if err != nil {
				fmt.Println(err.Error())
				break
			}
		}
		fileStream := bytes.Join(fil, []byte(""))
		blogContent := common.FileUtil.MarkdownToHTML(string(fileStream))
		fileContentList = append(fileContentList, blogContent)
		pictureList := c.GetString("pictureList")
		var list []maps.PictureListMap
		err := json.Unmarshal([]byte(pictureList), &list)
		if err != nil {
			panic(err)
		}
		pictureMap := map[string]string{}
		for _, item := range list {
			if systemConfig.PicturePriority == "1" {
				pictureMap[item.FileOldName] = item.QiNiuUrl
			} else {
				c.ErrorWithMessage("暂不支持其他存储")
				return
			}
		}
		matchUrlMap := map[string]string{}
		var matchList []string
		for _, blogContent := range fileContentList {
			if common.CheckPicture(blogContent) {
				matchList = append(matchList, blogContent)
			}
		}
		if len(matchList) > 0 {
			for _, matchStr := range matchList {
				splitList := strings.Split(matchStr, `\`)
				if len(splitList) >= 5 {
					var pictureUrl string
					if strings.Index(matchStr, "alt") > strings.Index(matchStr, "src") {
						pictureUrl = splitList[1]
					} else {
						pictureUrl = splitList[3]
					}
					if !strings.HasPrefix(pictureUrl, "http") {
						for key, value := range pictureMap {
							if strings.Contains(pictureUrl, key) {
								if systemConfig.PicturePriority == "1" {
									matchUrlMap[pictureUrl] = systemConfig.QiNiuPictureBaseUrl + value
								} else {
									c.ErrorWithMessage("暂不支持其他存储")
									return
								}
								break
							}
						}
					}
				}
			}
		}
		blogSort := service.BlogSortService.GetTopOne()
		tag := service.TagService.GetTopTag()
		picture := service.PictureService.GetTopOne()
		if blogSort == (models.BlogSort{}) || tag == (models.Tag{}) || picture == (models.Picture{}) {
			c.ErrorWithMessage("使用本地上传，请先确保博客分类，博客标签，博客图片中含有数据")
			return
		}
		admin := c.GetMeService()
		var blogList []models.Blog
		var count int
		projectName, _ := service.SysParamService.GetSysParamsValueByKey("PROJECT_NAME")
		var levels []string
		for _, content := range fileContentList {
			if len(matchUrlMap) > 0 {
				for key, value := range matchUrlMap {
					content = strings.ReplaceAll(content, key, value)
				}
			}
			blog := models.Blog{
				Uid:          xid.New().String(),
				BlogSortUid:  blogSort.Uid,
				TagUid:       tag.Uid,
				AdminUid:     admin.Uid,
				Author:       admin.NickName,
				ArticlesPart: projectName,
				Level:        0,
				Title:        fileNameList[count],
				Summary:      fileNameList[count],
				Content:      content,
				FileUid:      picture.FileUid,
				IsOriginal:   "1",
				IsPublish:    "0",
				OpenComment:  "1",
				Type:         "0",
			}
			blogList = append(blogList, blog)
			count++
			levels = append(levels, "BLOG_LEVEL:"+strconv.Itoa(blog.Level))
		}
		common.DB.Create(blogList)
		common.RedisUtil.MultiDelete(levels)
		c.SuccessWithMessage("新增成功")
	}
}


/**
* 同步到wordpress
*/
func (c *BlogSpiderRestApi) UploadWordpressBlog(){
	var blogSpiderList []models.BlogSpider
	common.DB.Where("is_publish=3").Find(&blogSpiderList)
	for _, item := range blogSpiderList {
		var blogSpider models.BlogSpider
		common.DB.Where("uid=?", item.Uid).Find(&blogSpider)
		fmt.Println("循环", item.Title)
		isSuccess  := service.BlogSpiderService.SaveSinglePostWp(item)
		if isSuccess {
			fmt.Println("成功推送", item.Title)
			blogSpider.IsPublish = "2"
			err1 := common.DB.Save(&blogSpider).Error
			if err1 != nil {
				fmt.Println(err1)
			}
		}
	}
	c.SuccessWithMessage("更新成功")
}

func (c *BlogSpiderRestApi) DeleteBatch() {
	var blogVOList []vo.BlogVO
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &blogVOList)
	if err != nil {
		panic(err)
	}
	if len(blogVOList) == 0 {
		c.ErrorWithMessage("参数错误")
	}
	var uidList []string
	var s []string
	for _, item := range blogVOList {
		uidList = append(uidList, item.Uid)
		s = append(s, item.Uid+",")
	}
	var blogList []models.Blog
	common.DB.Find(&blogList, uidList)
	save := common.DB.Model(&blogList).Select("status").Update("status", 0).Error
	if save == nil {
		service.SubjectItemService.DeleteBatchSubjectItemByBlogUid(uidList)
	}
	c.SuccessWithMessage("删除成功")
}

func (c *BlogSpiderRestApi) EditBatch() {
	var blogVOList maps.WpPathMap
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &blogVOList)
	if err != nil {
		panic(err)
	}
	fmt.Println(blogVOList.BlogSpiderList)
	if len(blogVOList.BlogSpiderList) == 0 {
		c.ErrorWithMessage("参数错误")
	}
	var blogUidList []string
	blogVOMap := make(map[string]vo.BlogSpiderVO)
	for _, item := range blogVOList.BlogSpiderList {
		blogUidList = append(blogUidList, item.Uid)
		blogVOMap[item.Uid] = item
	}
	var blogSpiderList []models.BlogSpider
	common.DB.Find(&blogSpiderList, blogUidList)
	for i, blog := range blogSpiderList {
		blogVO := blogVOMap[blog.Uid]
		if !reflect.DeepEqual(blogVO, vo.BlogSpiderVO{}) {
			blogSpiderList[i].Author = blogVO.Author
			blogSpiderList[i].ArticlesPart = blogVO.ArticlesPart
			blogSpiderList[i].Title = blogVO.Title
			blogSpiderList[i].Summary = blogVO.Summary
			blogSpiderList[i].Content = blogVO.Content
			blogSpiderList[i].TagUid = blogVO.TagUid
			blogSpiderList[i].BlogSortUid = blogVOList.BlogSortUid
			blogSpiderList[i].IsOriginal = blogVO.IsOriginal
			blogSpiderList[i].IsPublish = blogVO.IsPublish
			blogSpiderList[i].Sort = blogVO.Sort

		}
	}
	common.DB.Save(&blogSpiderList)
	c.SuccessWithMessage("更新成功")
}

func (c *BlogSpiderRestApi) GenerateQrcode() {
	// 1、获取所有文章数据
	blogs := []models.Blog{}
	common.DB.Find(&blogs)

	//fmt.Print(blogs)
	//systemConfig := c.GetSystemConfig()
	appId, _ := beego.AppConfig.String("app_id")
	appSecret, _ := beego.AppConfig.String("app_secret")

	for _, value := range blogs {
		fmt.Print(value.Oid)
		if strconv.Itoa(value.Oid) != "" {
			accesstoken, _ := service.WechatService.GetAccessToken(appId, appSecret)
			pictureurl, _ := service.WechatService.GetWxacodeUnlimit(accesstoken, "oid="+strconv.Itoa(value.Oid), "qrcode_viewid_"+strconv.Itoa(value.Oid), "pages/info/home")
			fmt.Println(pictureurl)
			time.Sleep(1400000000) // 1.4 秒
		}
	}

	// 2、检测文件是否存在：如果存在就不重新生成；如果不存在就重新生成；

	/*systemConfig := c.GetSystemConfig()
	accesstoken, err := service.WechatService.GetAccessToken("wxcd3c8104876a0436", "5c621d57018c0a38a7ed103035ce29b8")
	pictureurl, err := service.WechatService.GetWxacodeUnlimit(accesstoken, "a=2", "qrcode_viewid_"+strconv.Itoa(100), "pages/index/index")
	fmt.Println(pictureurl)
	base.L.Print("获取首页排行博客accesstoken" + accesstoken)
	base.L.Print(err)
	base.L.Print("获取首页最新的博客")
	*/
	//c.SuccessWithData(blogs)
	c.SuccessWithMessage("生成成功")
}

func (c *BlogSpiderRestApi) SyncUpdate(){
	start := time.Now() // 获取当前时间


	//c.SuccessWithMessage("todo")
	var wpForm maps.WpForm
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &wpForm)
	//url := c.GetString("url")
	url := wpForm.Url
	u, _ := urlUtil.Parse(url)
	fileDir := "/shbimages/"+ strings.Replace(u.Host, ".", "_", -1)
	httpHost := "http://www.shanhubei.com"
	if wpForm.HttpHost !="" {
		httpHost = wpForm.HttpHost
	}
	sleepTime := 0
	if wpForm.SleepTime != 0 {
		sleepTime = wpForm.SleepTime
	}
	//1、单独更新；2、批量更新
	resp, err := http.Get(url)
	if err != nil {
		c.ThrowError("0", "请携带url参数")
		fmt.Println(err)
	}
	defer resp.Body.Close()
	header := resp.Header
	totalPage,_ := strconv.Atoi(header["X-Wp-Totalpages"][0])
	//totalPage := 10
	baseUrl := u.Scheme + "://"+ u.Host +"/wp-json/wp/v2/posts/"
	//baseUrl := url
	//requestUrl := baseUrl + 
	body, err := ioutil.ReadAll(resp.Body)
	//fmt.Println(string(body))
	if err != nil {
		fmt.Println(err)
	}
	var jsonWpObjs []maps.WpMap
	json.Unmarshal([]byte(body), &jsonWpObjs)

	for _, jsonWpObj := range jsonWpObjs {
		//fmt.Println(jsonWpObj.Id)
		result := service.BlogService.SavePost(baseUrl + strconv.Itoa(jsonWpObj.Id), fileDir,httpHost,wpForm.BlogSortUid)
		fmt.Println(result)
	}

	if totalPage > 1 {
		fmt.Println("totalPage=",totalPage)
		page := 2
		for page =2; page <= totalPage; page ++{
			var pageUrl string
			if strings.Contains(url,"?") {
				pageUrl = url+"&page=" + strconv.Itoa(page)
			}else{
				pageUrl = url+"?page=" + strconv.Itoa(page)
			}
			//fmt.Println("pageUrl=",pageUrl)
			postlistView, err := http.Get(pageUrl)
			if err != nil {
				c.ThrowError("0", "请携带url参数2")
				fmt.Println(err)
			}
			defer postlistView.Body.Close()
			bodyview, _ := ioutil.ReadAll(postlistView.Body)
			jsonWpObjs = []maps.WpMap{}
			json.Unmarshal([]byte(bodyview), &jsonWpObjs)
			for _, jsonWpObj := range jsonWpObjs {
				c.Wg.Add(1)
				postUrl := baseUrl + strconv.Itoa(jsonWpObj.Id)
				go func(){
					fmt.Println("postUrl=",postUrl)
					//result := service.BlogService.SavePost(postUrl,fileDir,httpHost)
					fmt.Println(service.BlogService.SavePost(postUrl,fileDir,httpHost,wpForm.BlogSortUid))
					time.Sleep(time.Duration(sleepTime)*time.Second)
					c.Wg.Done()
				}()
			}
			c.Wg.Wait()
		}
	}
	elapsed := time.Since(start)
    fmt.Println("该函数执行完成耗时：", elapsed)
	c.SuccessWithMessage("todo")
}


func (c *BlogSpiderRestApi) SyncUpdateWp(){
	start := time.Now() // 获取当前时间

	//c.SuccessWithMessage("todo")
	var wpForm maps.WpForm
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &wpForm)
	//url := c.GetString("url")
	url := wpForm.Url
	u, _ := urlUtil.Parse(url)
	fileDir := "/shbimages/"+ strings.Replace(u.Host, ".", "_", -1)
	httpHost := "http://www.shanhubei.com"
	if wpForm.HttpHost !="" {
		httpHost = wpForm.HttpHost
	}
	sleepTime := 0
	if wpForm.SleepTime != 0 {
		sleepTime = wpForm.SleepTime
	}
	//1、单独更新；2、批量更新
	resp, err := http.Get(url)
	if err != nil {
		c.ThrowError("0", "请携带url参数")
		fmt.Println(err)
	}
	defer resp.Body.Close()
	header := resp.Header
	totalPage,_ := strconv.Atoi(header["X-Wp-Totalpages"][0])
	//totalPage := 10
	baseUrl := u.Scheme + "://"+ u.Host +"/wp-json/wp/v2/posts/"
	//baseUrl := url
	//requestUrl := baseUrl + 
	body, err := ioutil.ReadAll(resp.Body)
	//fmt.Println(string(body))
	if err != nil {
		fmt.Println(err)
	}
	var jsonWpObjs []maps.WpMap
	json.Unmarshal([]byte(body), &jsonWpObjs)

	//page=1 直接用以前的
	for _, jsonWpObj := range jsonWpObjs {
		//fmt.Println(jsonWpObj.Id)
		result := service.BlogService.SavePostWp(baseUrl + strconv.Itoa(jsonWpObj.Id), fileDir,httpHost,wpForm.BlogSortUid)
		fmt.Println(result)
	}

	if totalPage > 1 {
		fmt.Println("totalPage=",totalPage)
		//page=2
		page := 2
		for page =2; page <= totalPage; page ++{
			var pageUrl string
			if strings.Contains(url,"?") {
				pageUrl = url+"&page=" + strconv.Itoa(page)
			}else{
				pageUrl = url+"?page=" + strconv.Itoa(page)
			}
			//fmt.Println("pageUrl=",pageUrl)
			postlistView, err := http.Get(pageUrl)
			if err != nil {
				c.ThrowError("0", "请携带url参数2")
				fmt.Println(err)
			}
			defer postlistView.Body.Close()
			bodyview, _ := ioutil.ReadAll(postlistView.Body)
			jsonWpObjs = []maps.WpMap{}
			json.Unmarshal([]byte(bodyview), &jsonWpObjs)
			for _, jsonWpObj := range jsonWpObjs {
				c.Wg.Add(1)
				postUrl := baseUrl + strconv.Itoa(jsonWpObj.Id)
				go func(){
					fmt.Println("postUrl=",postUrl)
					//result := service.BlogService.SavePost(postUrl,fileDir,httpHost)
					fmt.Println(service.BlogService.SavePostWp(postUrl,fileDir,httpHost,wpForm.BlogSortUid))
					time.Sleep(time.Duration(sleepTime)*time.Second)
					c.Wg.Done()
				}()
			}
			c.Wg.Wait()
		}
	}
	elapsed := time.Since(start)
    fmt.Println("该函数执行完成耗时：", elapsed)
	c.SuccessWithMessage("todo")
}
